import router from '@/router'
import {post} from '@/util/request'
import moment from 'moment'

export default {
	namespace:'accidentList',
	namespaced:true,
	state:{
		isLoading:false,
		isModalLoading:false,
		isSubmiting:false,
		pageData:{},
		viewData:{}
	},
	mutations:{
		changeLoading(state,v){
			state.isLoading = v;
		},
		changeModalLoading(state,v){
			state.isModalLoading = v;
		},
		changeSubmit(state,v){
			state.isSubmiting = v;
		},
		changePagedata(state,v){
			state.pageData = v;
		},
		changeViewData(state,v){
			state.viewData = v;
		}
	},
	actions:{
		async reload({commit}, data) {
			commit('changeLoading',true);
			let res = await post('/base/accident/list',data);
			res.data.data.thisPageElements.forEach((v,i) => {
        let accidentTypeStr = '';
        switch (Number.parseInt(v.accidentType)) {
          case 1: accidentTypeStr = '双车剐蹭'; break;
          case 2: accidentTypeStr = '部件失效'; break;
					case 3: accidentTypeStr = '追尾碰撞'; break;
          case 4: accidentTypeStr = '车辆颠覆'; break;
        }
        v.accidentTypeStr = accidentTypeStr;
				v.incidentTimeStr = moment(v.incidentTime).format('YYYY-MM-DD');
				v.lossMoneyStr = (v.lossMoney/100).toFixed(2);
				let accidentLevelStr = '';
				switch (Number.parseInt(v.accidentLevel)) {
          case 1: accidentLevelStr = '轻微事故'; break;
          case 2: accidentLevelStr = '一般事故'; break;
					case 3: accidentLevelStr = '严重事故'; break;
          case 4: accidentLevelStr = '特大事故'; break;
        }
				v.accidentLevelStr = accidentLevelStr;
				v.isProcessStr = v.isProcess===0?'未处理':'已处理';
      });
			commit('changePagedata',res.data.data);
			commit('changeLoading',false);
		},
		deleteAccident({commit,state},id){
			commit('changeModalLoading',true);
			return post('/base/accident/deleteAccidentById',{id}).then(data=>{
				commit('changeModalLoading',false);
				return data;
			})
		},
		async loadView({commit},id){
			commit('changeModalLoading',true);
			let res = await post('/base/accident/findAccidentById',{id});
			commit('changeViewData',res.data.data);
			commit('changeModalLoading',false);
		},
		add({commit,state},form){
			commit('changeSubmit',true);
			return post('/base/accident/addAccident',form).then(data=>{
				commit('changeSubmit',false);
				return data;
			})
		},
		update({commit,state},form){
			commit('changeSubmit',true);
			return post('/base/accident/updateAccident',form).then(data=>{
				commit('changeSubmit',false);
				return data;
			})
		}
	},
	subscriptions({dispatch},router){
		//给路由加监听一旦进入module绑定的页面时dispatch初始化action
		router.afterEach((to,from)=>{
			if(to.name === this.namespace){
				// dispatch('reload',{})
			}
		})
	}
}
